package com.caigou.service.impl;

import com.caigou.bean.cms.entity.BanWords;
import com.caigou.bean.cms.param.BanWordsParam;
import com.caigou.enums.FailResultEnum;
import com.caigou.enums.ResponseEnum;
import com.caigou.mapper.cms.BanWordsMapper;
import com.caigou.service.IBanWordsService;
import com.caigou.util.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BanWordsServiceImpl implements IBanWordsService {
    @Resource
    private BanWordsMapper banWordsMapper;

    @Override
    public Result<Integer> addBanWords(BanWordsParam param) {
        //先查询是否有重复的敏感词
        int num = 0;
        String[] split = param.getVocabulary().split(",");
        if (split.length > 0) {
            for (String s : split) {
                if (s.length() <= 15) {
                    BanWords banWord = banWordsMapper.findBanWordsByKeyword(s.trim());
                    if (banWord == null) {
                        BanWords banWords = new BanWords();
                        banWords.setVocabulary(s.trim());
                        banWords.setSubstituteWord(param.getSubstituteWord());
                        banWords.setCreateTime(new Date());
                        int addNum = banWordsMapper.insertSelective(banWords);
                        if (addNum == 1) {
                            num++;
                        }
                    }
                }
            }
        }
        int errorNum = split.length - num;
        return Result.custom(200, "成功添加" + num + "条,失败" + errorNum + "条", num);
    }

    @Override
    public Result<Integer> deleteBanWords(BanWordsParam param) {
        if (param.getId() == null) {
            return Result.error(ResponseEnum.NO_WORDS_ID);
        }
        int deleteNum = banWordsMapper.deleteByPrimaryKey(param.getId());
        if (deleteNum == 0) {
            return Result.error(ResponseEnum.HAS_DELETE);
        }
        return Result.success(deleteNum);
    }

    @Override
    public Result<PageInfo<BanWords>> queryBanWords(BanWordsParam param) {
        PageHelper.startPage(param.getPage(), param.getSize());
        PageInfo<BanWords> usersPageInfo = new PageInfo<>(banWordsMapper.findBanWordsByVagueKeyword(param.getKeyword()));

        return Result.success(usersPageInfo);

    }

    @Override
    public Result<Integer> updateBanWords(BanWordsParam param) {
        if (param.getId() == null) {
            return Result.error(ResponseEnum.NO_WORDS_ID);
        }
        if (StringUtils.isEmpty(param.getVocabulary())) {
            return Result.error(ResponseEnum.PRIVILEGE_ERROR);
        }


        BanWords banWord = banWordsMapper.findBanWordsByKeyword(param.getVocabulary().trim());
        if (banWord != null) {
            return Result.error(FailResultEnum.REPEAT_VOCABULARY);
        }

        BanWords banWords = new BanWords();
        banWords.setId(param.getId());
        banWords.setVocabulary(param.getVocabulary().trim());
        banWords.setSubstituteWord(!StringUtils.isEmpty(param.getSubstituteWord()) ? param.getSubstituteWord().trim() : null);
        return Result.success(banWordsMapper.updateByPrimaryKeySelective(banWords));
    }

    @Override
    public String getPureWords(String comments) {
        List<BanWords> list = banWordsMapper.findAllBanWords();
        String regex = list.stream().map(BanWords::getVocabulary).collect(Collectors.joining("|"));
        return comments.replaceAll(regex, "***");
    }
}
